<template>
  <div class="other-item">
    <div class="flex flex-row justify-start items-center gap-2 p-2 box-border">
      <span>{{ formData.label }}</span>
      <span>{{ showValue }}</span>
      <span>{{ formData.suffix }}</span>
    </div>
    <el-dialog title="编辑内容" :model-value="isRevealed" show-close append-to-body @close="handleClose">
      <el-form :model="formData" label-width="auto" label-position="left">
        <el-form-item label="设备类型">
          <el-select v-model="formData.type">
            <el-option :value="EQUIPMENT_TYPE.PRECIPITATE_POOL" label="沉淀池"></el-option>
            <el-option :value="EQUIPMENT_TYPE.DECAY_POOL" label="衰变池"></el-option>
            <el-option :value="EQUIPMENT_TYPE.AIR_PROBE" label="空气探头"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备名称">
          <el-select v-model="formData.index">
            <el-option v-for="(item,index) in deviceList.filter(item=>item.equipmentType===formData.type)"
                       :key="item.id as number"
                       :value="index" :label="item.equipmentName as string">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="显示内容">
          <el-input v-model="formData.label"></el-input>
        </el-form-item>
        <el-form-item label="字段名称">
          <el-select v-model="formData.field">
            <el-option v-for="key in keyList" :key="key" :label="key" :value="key"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="后缀">
          <el-input v-model="formData.suffix"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="handleConfirm">保存</el-button>
        <el-button type="info" @click="handleClose">取消</el-button>
      </template>
    </el-dialog>
  </div>

</template>

<script setup lang="ts">
import { computed, inject, ref } from 'vue'
import { Node } from '@antv/x6'
import { ElButton, ElDialog, ElForm, ElFormItem, ElOption, ElSelect,ElInput } from 'element-plus'
import { useConfirmDialog } from '@vueuse/core'
import { storeToRefs } from 'pinia'
import { useDeviceData } from '@/stores/useWebSocket'
import { EQUIPMENT_TYPE } from '@/utils/enums'

const { deviceList } = storeToRefs(useDeviceData())

const getNode = inject('getNode') as () => Node
const node = getNode()
node.removePorts()
const nodeData = ref(node.getData() || {})
node.setData({
  dialog: false
}, {
  silent: true
})
const { isRevealed, confirm, reveal, cancel, onConfirm, onReveal } = useConfirmDialog()

node.on('change:data', ({ current }) => {
  reveal(current)
})

const formData = ref<{
  index?: number,
  type?: number,
  field?: string,
  label?: string,
  suffix?: string
}>(node.getData() || { index: 0 })
onReveal((data) => {
  formData.value = data
})

onConfirm((data) => {
  nodeData.value = data
  node.setData(data, { silent: true })
})

const handleClose = function() {
  cancel()
  node.setData({
    dialog: false
  }, { silent: true })
}
const handleConfirm = function() {
  confirm(node.getData())
}

const device = computed(() => {
  return deviceList.value.filter((item) => item.equipmentType === formData.value.type)[formData.value?.index || 0]
})

const keyList = computed(() => {
  return Object.keys(device.value || {})
})

const showValue = computed(() => {
  return formData.value.field ? device.value?.[formData.value.field] : ''
})

</script>

<style scoped lang="scss">
.other-item {
  height: 100%;
  width: fit-content;
  min-width: 100%;

  box-shadow: 0 0 15px 0 rgba(46, 112, 236, 0.3);
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid #387af6 !important;
}
</style>
